package com.easylinkin.linkappapi.grid.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.core.models.Operator;
import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.exceptions.ExceptionEnum;
import com.easylinkin.linkappapi.grid.copy.GridReportWeekDetailCopy;
import com.easylinkin.linkappapi.grid.entity.GridReportWeekDetail;
import com.easylinkin.linkappapi.grid.entity.GridReportWeekUser;
import com.easylinkin.linkappapi.grid.mapper.GridReportDayDetailMapper;
import com.easylinkin.linkappapi.grid.mapper.GridReportWeekDetailMapper;
import com.easylinkin.linkappapi.grid.mapper.GridReportWeekMapper;
import com.easylinkin.linkappapi.grid.models.dto.GridReportWeekDetailSaveDTO;
import com.easylinkin.linkappapi.grid.models.query.GridReportDayDetailWeekQuery;
import com.easylinkin.linkappapi.grid.models.vo.*;
import com.easylinkin.linkappapi.grid.service.IGridReportWeekDetailService;
import com.easylinkin.linkappapi.grid.service.IGridReportWeekUserService;
import com.easylinkin.linkappapi.tenant.entity.LinkappTenant;
import com.easylinkin.linkappapi.tenant.mapper.LinkappTenantMapper;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 网格周 报填写详情 服务实现类
 *
 * @author kuyujie
 * @since 2024-05-22
 */
@Slf4j
@Setter(onMethod_ = @Resource)
@Service
public class GridReportWeekDetailServiceImpl extends ServiceImpl<GridReportWeekDetailMapper, GridReportWeekDetail> implements IGridReportWeekDetailService {

    private LinkappTenantMapper linkappTenantMapper;
    private GridReportWeekMapper gridReportWeekMapper;
    private GridReportDayDetailMapper gridReportDayDetailMapper;

    private IGridReportWeekUserService gridReportWeekUserService;

    @Override
    public void saveData(GridReportWeekDetailSaveDTO dto) {
        Operator operator = dto.getOperator();

        GridReportWeekUser reportWeekUser = gridReportWeekUserService.getByReportIdUserId(dto.getReportId(), operator.getUserId());
        if (ObjectUtil.isNull(reportWeekUser)) {
            log.error("您不是填报人 dto:{}", JSON.toJSONString(dto));
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.DATA_CHECK_ERROR, "您不是填报人");
        }

        if (ObjectUtil.isNotNull(reportWeekUser.getSubmitTime())) {
            log.error("您已填报，请勿重复填报 reportDayUser:{}", JSON.toJSONString(reportWeekUser));
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.DATA_CHECK_ERROR, "您已填报，请勿重复填报");
        }

        GridReportWeekDetail entity = GridReportWeekDetailCopy.copyAndNew(dto);
        entity.setReportUserId(reportWeekUser.getId());
        entity.setUserId(reportWeekUser.getUserId());

        boolean dbFlag = this.save(entity);
        if (!dbFlag) {
            log.error("填报失败，保存填报数据失败");
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.EXCEPTION_CODE_500, "填报失败");
        }

        dbFlag = gridReportWeekUserService.updateSubmit(entity);
        if (!dbFlag) {
            log.error("填报失败，填报时间更新失败");
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.EXCEPTION_CODE_500, "填报失败");
        }
    }

    @Override
    public GridReportWeekDetailCollectVO getDetailCollect(@NotNull(message = "周报ID不能为空") Long reportId) {
        GridReportWeekInfoVO reportWeekInfoVO = gridReportWeekMapper.selectInfoById(reportId);
        if (ObjectUtil.isNull(reportWeekInfoVO)) {
            throw new BusinessException(ExceptionEnum.CommonExceptionEnum.DATA_CHECK_ERROR, "周报不存在");
        }

        String tenantId = reportWeekInfoVO.getTenantId();

        GridReportWeekDetailCollectVO vo = new GridReportWeekDetailCollectVO();
        vo.setReportId(reportId);

        GridReportWeekDetailInfoVO weekDetailInfoVO = baseMapper.selectOneByReportId(reportId);
        GridReportWeekDetailCopy.copy(weekDetailInfoVO, vo);

        // 获取项目信息
        LinkappTenant linkappTenant = linkappTenantMapper.getById(tenantId);
        GridReportWeekDetailCopy.copy(linkappTenant, vo);

        // 汇总日报的数据
        GridReportDayDetailWeekCollectVO dayCollectVO = new GridReportDayDetailWeekCollectVO();
        List<GridReportDayDetailInfoVO> dayDetailInfoVOList = gridReportDayDetailMapper.selectListByWeekQuery(GridReportDayDetailWeekQuery.builder()
                .tenantId(tenantId)
                .start(reportWeekInfoVO.getWeekStart())
                .end(reportWeekInfoVO.getWeekEnd())
                .build());
        dayDetailInfoVOList.parallelStream().forEach(dayCollectVO::collect);
        vo.setDayCollectVO(dayCollectVO);

        return vo;
    }
}